﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using VASJ.Model;
using VASJ.Presentation.HeThong;
using VASJ.Properties;

namespace VASJ.Presentation
{
    public partial class FrmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        #region Properties

        #endregion

        #region Methods
        private void ShowChildForm(XtraForm frmType, string typeName)
        {

        }

        private void ShowChildForm(XtraForm frm)
        {
            CloseExistingForm(frm.GetType());
            CreateChildForm(frm);
        }

        public void CreateChildForm(XtraForm form)
        {
            form.MdiParent = this;
            form.Show();
        }

        public void CloseExistingForm(Type type)
        {
            foreach (XtraForm item in this.MdiChildren)
            {
                if (item.GetType() == type)
                {
                    item.Close();
                }
            }
        }

        #endregion

        #region Events
        public FrmMain()
        {
            InitializeComponent();

            FrmNgayBatDau frm = new FrmNgayBatDau();
            CreateChildForm(frm);
        }

        private void iNgayBatDau_ItemClick(object sender, ItemClickEventArgs e)
        {
            XtraForm frm = new FrmNgayBatDau();
            ShowChildForm(frm);
        }

        private void iCacNhom_ItemClick(object sender, ItemClickEventArgs e)
        {
            XtraForm frm = new FrmCacNhom();
            ShowChildForm(frm);
        }

        private void iDanhMuc_ItemClick(object sender, ItemClickEventArgs e)
        {

        }
        #endregion

        private void iSubNgayBatDau_ItemClick(object sender, ItemClickEventArgs e)
        {
            XtraForm frm = new FrmNgayBatDau();
            ShowChildForm(frm);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {

        }

        #region Automatic create Ribbon controls

        private void AutomaticAddMenu()
        {
            try
            {
                LoadAllMenus();

                RibbonPage ribbonPage = new RibbonPage("File");
                RibbonPageGroup ribbonPageGroup = new RibbonPageGroup("Start Date");
                BarButtonItem barButtonItem = null;

                if (MenuManager.MenuBarButtonInstant != null)
                {
                    List<MenuBarButtonItem> listMenuBarButtons = MenuManager.MenuBarButtonInstant.MenuBarButtons;

                    if (listMenuBarButtons != null && listMenuBarButtons.Count > 0)
                    {
                        foreach (MenuBarButtonItem mB in listMenuBarButtons)
                        {
                            barButtonItem = new BarButtonItem();
                            ribbonPageGroup.ItemLinks.Add(barButtonItem);
                            barButtonItem.Caption = mB.Caption;
                            barButtonItem.Tag = mB;
                            
                            Bitmap image = (Bitmap)Resources._360_16x16.Clone();
                            image.MakeTransparent(Color.Fuchsia);
                            barButtonItem.Glyph = image;

                            barButtonItem.ItemClick += new ItemClickEventHandler(barButtonItem_ItemClick);
                        }
                    }

                    ribbonPage.Groups.AddRange(new DevExpress.XtraBars.Ribbon.RibbonPageGroup[] { ribbonPageGroup });
                    this.ribbon.Pages.Add(ribbonPage);
                }
            }
            catch (Exception ex)
            {
                StackFrame stackFrame = new StackFrame();
                Logger.Log.Error(string.Format("{0}.{1}", stackFrame.GetMethod().DeclaringType.Name, stackFrame.GetMethod().Name));
                Logger.Log.Error(ex.Message);
                if (ex.InnerException != null) Logger.Log.Error(ex.InnerException.ToString());
                throw ex;
            }
        }

        private void LoadAllMenus()
        {
            try
            {
                List<MenuBarButtonItem> menuBarButtonItems = DALHelper.GetAllMenus();
                if (menuBarButtonItems != null && menuBarButtonItems.Count > 0)
                {
                    MenuManager.MenuBarButtonInstant.MenuBarButtons = menuBarButtonItems;
                    MenuManager.Save();
                }
            }
            catch (Exception ex)
            {
                StackFrame stackFrame = new StackFrame();
                Logger.Log.Error(string.Format("{0}.{1}", stackFrame.GetMethod().DeclaringType.Name, stackFrame.GetMethod().Name));
                Logger.Log.Error(ex.Message);
                if (ex.InnerException != null) Logger.Log.Error(ex.InnerException.ToString());
                throw ex;
            }
        }

        #endregion

        void barButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            BarButtonItem m_BarButtonItem = e.Item as BarButtonItem;

            MenuBarButtonItem menuButton = m_BarButtonItem.Tag as MenuBarButtonItem;

            Process.Start(menuButton.Action);
        }
    }
}